<?php
declare(strict_types=1);

namespace App\Controller;

use App\ApiBaseController;
use App\Common;
use App\Model\Friend;
use App\Model\Group;
use App\Model\GroupUser;
use App\Model\Msg;
use App\Model\UserBind;
use Hyperf\HttpServer\Annotation\AutoController;
use Psr\Http\Message\ResponseInterface;
use Hyperf\Di\Annotation\Inject;

/**
 * @AutoController
 */
class V1Controller extends ApiBaseController
{
    /**
     * @Inject
     * @var Common
     */
    private $_common;

    /**
     * 登录（绑定）到 Im
     */
    public function login(): ResponseInterface
    {
        $post = $this->request->post();
        $res_msg = UserBind::updateUser($this->_appid, $post);

        if (is_array($res_msg))
            return $this->res_success($res_msg);

        return $this->res_error($res_msg);
    }

    /**
     * 发送消息
     */
    public function sendmsg(): ResponseInterface
    {
        $content    = $this->request->post('content');
        $account    = $this->request->post('account');
        $to_account = $this->request->post('to_account');

        if (empty($content)) return $this->res_json(-1001,[],'content is null');
        if (empty($account)) return $this->res_json(-1001,[],'account is null');
        if (empty($to_account)) return $this->res_json(-1001,[],'to_account is null');

        // 向 WebSocket 服务端发送消息
        $info = [
           'tag' => 'sendtext',
           'data' => [
               'account' => $account,
               'to_account' => $to_account,
               'content' => $content,
           ]
        ];
        return $this->res_response($info);
    }

    /**
     * ----------------------------------------------------------
     * 好友相关
     * ----------------------------------------------------------
     */

    /**
     * 获取好友列表
     * @return ResponseInterface
     */
    public function fried_list(): ResponseInterface
    {
        $post = $this->request->post();

        $data = Friend::getList($this->_appid, $post);

        if (is_array($data))
            return $this->res_success($data);

        return $this->res_error($data);

    }

    /**
     * 添加好友
     * @return ResponseInterface
     */
    public function fried_add(): ResponseInterface
    {
        $post = $this->request->post();

        $res_err = Friend::add($this->_appid, $post);
        if ($res_err)
            return $this->res_error($res_err);

        return $this->res_success();

    }

    /**
     * 删除好友
     * @return ResponseInterface
     */
    public function fried_del(): ResponseInterface
    {
        $post = $this->request->post();

        $res_err = Friend::delFried($this->_appid, $post);
        if ($res_err)
            return $this->res_error($res_err);

        return $this->res_success();

    }

    /**
     * 好友-同意
     * @return ResponseInterface
     */
    public function fried_agree(): ResponseInterface
    {
        $post = $this->request->post();

        $res_err = Friend::agree($this->_appid,$post);
        if ($res_err)
            return $this->res_error($res_err);

        return $this->res_success();

    }

    /**
     * 修改用户信息
     * @return ResponseInterface
     */
    public function account_update(): ResponseInterface
    {
        $post = $this->request->post();

        $res_data = UserBind::updateFriendUser($this->_appid,$post);
        if (!is_array($res_data))
            return $this->res_error($res_data);

        return $this->res_success($res_data);

    }

    /**
     * 获取与好友的聊天记录
     * @return ResponseInterface
     */
    public function fried_msg_list(): ResponseInterface
    {
        $post = $this->request->post();

        $res_data = Msg::getList($this->_appid,$post);
        if (is_array($res_data))
            return $this->res_success($res_data);

        return $this->res_error($res_data);
    }

    /**
     * 获取有聊天记录的好友列表
     * @return ResponseInterface
     */
    public function get_fried_msg()
    {
        $post = $this->request->post();
        $res_data = Msg::getFriedMsg($this->_appid, $post);
        if (is_array($res_data))
            return $this->res_success($res_data);

        return $this->res_error($res_data);
    }



    /**
     * ----------------------------------------------------------
     * 群组相关
     * ----------------------------------------------------------
     */

    /**
     * 创建群组
     * @return ResponseInterface
     */
    public function group_create(): ResponseInterface
    {
        // 群主id
        $post = $this->request->post();

        $res_data = Group::createGroup($this->_appid, $post);
        if (is_array($res_data))
            return $this->res_success($res_data);

        return $this->res_error($res_data);
    }

    /**
     * 解散群组
     * @return ResponseInterface
     */
    public function group_del(): ResponseInterface
    {
        // 群 id 数组
        $g_ids = $this->request->post('g_ids');

        $res_data = Group::delGroup($this->_appid, $g_ids);
        if (is_array($res_data))
            return $this->res_success($res_data);

        return $this->res_error($res_data);
    }

    /**
     * 修改群信息
     * @return ResponseInterface
     */
    public function group_update(): ResponseInterface
    {
        // 群 id 数组
        $array = $this->request->post();

        $res_data = Group::updateGroup($this->_appid, $array);
        if (is_array($res_data))
            return $this->res_success($res_data);

        return $this->res_error($res_data);
    }

    /**
     * 群组-获取列表
     * @return ResponseInterface
     */
    public function group_list(): ResponseInterface
    {
        // 群主id
        $post = $this->request->post();

        $res_data = Group::listGroup($this->_appid, $post);
        if (is_array($res_data))
            return $this->res_success($res_data);

        return $this->res_error($res_data);
    }

    /**
     * 加入群组
     * @return ResponseInterface
     */
    public function group_add(): ResponseInterface
    {
        $g_id     = (int)$this->request->post('g_id');
        $accounts = $this->request->post('accounts');

        if ($g_id <= 0)
            return $this->res_json(-1001,[],'g_id is null');

        if (empty($accounts))
            return $this->res_json(-1001,[],'accounts is null');

        if (!is_array($accounts)){
            $accounts = json_decode($accounts, true);
        }

        $res_data = GroupUser::addGroup($this->_appid,$g_id,$accounts);
        if (is_array($res_data))
            return $this->res_success($res_data);

        return $this->res_error($res_data);
    }

    /**
     * 群组-获群成员
     * @return ResponseInterface
     */
    public function group_user_list(): ResponseInterface
    {
        // 群主id
        $post = $this->request->post();

        $res_data = GroupUser::getGroupUser($this->_appid, $post);
        if (is_array($res_data))
            return $this->res_success($res_data);

        return $this->res_error($res_data);
    }

    /**
     * 群组-删除群成员
     * @return ResponseInterface
     */
    public function group_user_del(): ResponseInterface
    {
        // 群主id
        $g_id     = $this->request->post('g_id');
        $accounts = $this->request->post('accounts');

        $res_data = GroupUser::delUser($this->_appid, $g_id, $accounts);
        if (is_array($res_data))
            return $this->res_success($res_data);

        return $this->res_error($res_data);
    }

    /**
     * 转让群
     * @return ResponseInterface
     */
    public function group_change_owner(): ResponseInterface
    {
        // 群 id 数组
        $g_id = $this->request->post('g_id');
        $to_account = $this->request->post('to_account');

        $res_data = Group::changeOwner($this->_appid, $g_id, $to_account);
        if (is_array($res_data))
            return $this->res_success($res_data);

        return $this->res_error($res_data);
    }


    public function get_after_msg()
    {
        $appid      = (string)$this->request->input('appid');
        $page       = (string)$this->request->input('page', 1);
        $account    = (string)$this->request->input('account');

        if (!$account) return $this->res_json(-1001,[],'account is null');

        $info = Msg::getAfterMsg($appid,$account,$page);
        return $this->res_json(0, $info);
    }
}